local sk__xionglve = fk.CreateSkill {

  name = "sk__xionglve",

  tags = {},

}

local U = require "packages/utility/utility"






sk__xionglve:addEffect("active", {
  name = "sk__xionglve",
  anim_type = "special",
  expand_pile = "sk_sr__sunquan_lve",
  derived_piles = "sk_sr__sunquan_lve",
  prompt = "#sk__xionglve",
  card_num = 1,
  can_use = function(self, player)
    return #player:getPile("sk_sr__sunquan_lve") > 0
  end,
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    local names = player:getViewAsCardNames(sk__xionglve.name, all_names)
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 and player:getPileNameOfId(to_select) == "sk_sr__sunquan_lve" then
      local card = Fk:getCardById(to_select)
      if card.type == Card.TypeEquip then
        return true
      else
        return card.type == Fk:cloneCard(self.interaction.data).type
      end
    end
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected_cards > 0 and self.interaction.data then
      if Fk:getCardById(selected_cards[1]).type == Card.TypeEquip then
        return #selected == 0 and #selected_cards == 1 and to_select ~= player and
            to_select:canMoveCardIntoEquip(selected_cards[1], false)
      else
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = sk__xionglve.name
        if card.skill:getMinTargetNum(player) == 0 then
          return false
        else
          return card.skill:targetFilter(to_select, selected, selected_cards, card)
        end
      end
    end
  end,
  feasible = function(self, player, selected, selected_cards, card)
    if #selected_cards > 0 and self.interaction.data then
      if Fk:getCardById(selected_cards[1]).type == Card.TypeEquip then
        return #selected_cards == 1 and #selected == 1
      else
        local card = Fk:cloneCard(self.interaction.data)
        card.skillName = sk__xionglve.name
        card:addSubcards(selected_cards)
        if player:canUse(card) and not player:prohibitUse(card) then
          return #selected_cards == 1 and card.skill:feasible(selected, selected_cards, player, card)
        end
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    if Fk:getCardById(effect.cards[1]).type == Card.TypeEquip then
      local target = effect.tos[1]
      room:moveCardIntoEquip(target, effect.cards[1], sk__xionglve.name, true, player)
    else
      local use = {
        from = player,
        tos = effect.tos,
        card = Fk:cloneCard(self.interaction.data),
      }
      use.card:addSubcards(effect.cards)
      use.card.skillName = sk__xionglve.name
      room:useCard(use)
    end
  end,
})

sk__xionglve:addEffect(fk.EventPhaseStart, {
  name = "#sk__xionglve_trigger",
  mute = true,
  --sk__xionglve,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__xionglve.name) and player.phase == Player.Draw
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "sk__xionglve")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__xionglve")
    room:notifySkillInvoked(player, "sk__xionglve", "special")
    local cards = room:getNCards(2)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, "sk__xionglve", nil, true, player)
    cards = table.filter(cards, function(id)
      return room:getCardArea(id) == Card.Processing
    end)
    if #cards == 0 then return end
    if player.dead then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, "sk__xionglve", nil, true)
      return
    end
    local card, _ = room:askToChooseCardsAndChoice(player, {
      cards = cards,
      choices = { "OK" },
      skill_name = "sk__xionglve",
      prompt = "#sk__xionglve-get",
      cancel_choices = {},
      min = 1,
      max = 1,
    })
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, "sk__xionglve", nil, true, player)
    cards = table.filter(cards, function(id)
      return room:getCardArea(id) == Card.Processing
    end)
    if #cards == 0 then return end
    if player.dead then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, "sk__xionglve", nil, true)
      return
    end
    player:addToPile("sk_sr__sunquan_lve", cards, true, "sk__xionglve", player.id)
    data.phase_end = true
  end,
})

return sk__xionglve
